package com.seeTools.next.controller.sys;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seeTools.next.api.entity.sys.SysRole;
import com.seeTools.next.api.entity.sys.SysRoleMenu;
import com.seeTools.next.common.core.PageQuery;
import com.seeTools.next.common.core.Result;
import com.seeTools.next.conf.ThreadLocalHolder;
import com.seeTools.next.service.sys.ISysRoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 系统角色 前端控制器
 * </p>
 *
 * @author FallenDust
 * @since 2023-05-10
 */
@RestController
@RequestMapping("/sysRole")
public class SysRoleController {
    @Resource
    private ISysRoleService service;

    /**
     * 分页查询
     *
     * @param pageQuery 查询条件
     * @return 结果
     */
    @SaCheckPermission("role.list")
    @PostMapping("/page")
    public Result<Page<SysRole>> page(@RequestBody PageQuery<SysRole> pageQuery) {
        Page<SysRole> pageInfo = service.pageInfo(pageQuery);
        return Result.state(pageInfo);
    }

    /**
     * 新增
     *
     * @param entity 实体
     * @return Result
     */
    @SaCheckPermission("role.add")
    @PostMapping("/add")
    public Result<Void> add(@RequestBody SysRole entity) {
        return Result.state(service.save(entity));
    }


    /**
     * 更新
     *
     * @param entity 实体
     * @return Result
     */
    @SaCheckPermission("role.edit")
    @PostMapping("/update")
    public Result<Void> update(@RequestBody SysRole entity) {
        return Result.state(service.updateById(entity));
    }

    /**
     * 删除
     *
     * @param entity 实体
     * @return Result
     */
    @SaCheckPermission("role.del")
    @PostMapping("/delete")
    public Result<Void> delete(@RequestBody SysRole entity) {
        return Result.state(service.removeById(entity));
    }


    /**
     * 详情
     *
     * @param id id
     * @return Result
     */
    @GetMapping("/info/{id}")
    public Result<SysRole> info(@PathVariable("id") Long id) {
        SysRole entity = service.getById(id);
        return Result.state(entity);
    }

    /**
     * 获取角色列表
     *
     * @return Result
     */
    @GetMapping("/roleList")
    public Result<List<SysRole>> roleList() {
        List<SysRole> roleList = service.getRoleListCache(ThreadLocalHolder.getTenantId());
        return Result.state(roleList);
    }

    /**
     * 获取角色菜单关联
     *
     * @param roleId
     * @return
     */
    @GetMapping("/getRoleMenuList/{roleId}")
    public Result<List<SysRoleMenu>> getRoleMenuList(@PathVariable("roleId") Long roleId) {
        List<SysRoleMenu> roleMenuList = service.getRoleMenuList(roleId);
        return Result.state(roleMenuList);
    }

    /**
     * 角色菜单权限分配
     *
     * @param sysRole
     * @return
     */
    @PostMapping("/assignMenus")
    public Result<Void> assignMenus(@RequestBody SysRole sysRole) {
        if (sysRole.getId() == null || sysRole.getMenuIdList() == null) {
            return Result.error("参数不完整[id]");
        }
        service.assignMenus(sysRole.getId(), sysRole.getMenuIdList());
        return Result.success();
    }
}
